<?php
class service {
	public $err1="101"; //无授权，拒绝连接
	public $err2="102"; //传输数据类型出错
	public $err3="103"; //传输数据格式出错
	public $err4="104"; //数据无法传输
	public $err5="105"; //数据为空或重叠
	public $err6="106"; // 数据服务出错，无法提交订单
	public $err7="107"; // 返回数据非XML格式
	public $err8="108"; // 网络中断
	public $err9="109"; // 重复查询
	public $err10="110"; // 重复提交
	public $err11="111"; // 数据含有非法字符
	public $err12="112"; // 手机格式错误
	public $err13="113"; // 错误的联票方式
	public $err14="114"; // 编码出错，无法继续	
	public $err15="115"; // 网站服务出错，请联系技术开发人员
	public $err16="116"; // 短信重发次数过多
	public $err17="117"; // 在线支付未支付成功无法发短信
	public $err18="118"; // 终端信息出错，请联系技术人员
	public $err19="119"; // 订单状态出错，请联系技术人员
	public $err20="120"; // 订单号已超出系统承载，无法生成
	public $err21="121"; // 状态参数出错
	public $err22="122"; // 订单号已存在，无法提交
	
	public $t1="uu_land";
    public $t2="uu_jq_ticket";
	public $t3="uu_ticket_pprice";
	public $t4="uu_jq_order";
	public $t5="uu_area";
	public $t6="uu_order_trace";
	public $t7="uu_ss_order";
	public $t21="uu_route";
	public $t22="uu_hotel";
	public $t23="uu_gifts";
    //private $auth=false;
	
//验证身份
	public function authrize($ac, $pw){
		$ac=urlencode($ac);
		$pw=urlencode($pw);
	
	    $str="select id,vtype from dataex where account=\"".$ac."\" and pwd=\"".$pw."\" limit 1";
        $GLOBALS['le']->query($str);
	    if ($GLOBALS['le']->fetch_assoc()) {
			$this->vtype=$GLOBALS['le']->f('vtype');
			return true;
		}else{
			return false;
		}
	}

//[综合]动态价格获取（产品ID，日期，模式[0XML 1单个价格 2单个最低价]，开始时间，结束时间，类型[0供应价 1零售价]）说明：时间段价格$date,$mode必须为默认空值并会跨时间集，是否需要获取库存1需要
	public function get_Dynamic_Price_Merge($pid, $date='', $mode=0, $sdate='', $edate='', $ptype=0, $get_storage=0) {
		//if ($ac=$this->authrize()){
		if (is_numeric($pid)===false) return "error1";
	
		//取单个价格
		$onday=date('w',strtotime($date));
		if ($mode==1 && $date){
			//特殊日价格
			$q_s_price=($ptype==0)?'s_price':'l_price as s_price';
			$str="select $q_s_price,storage from uu_product_price where start_date<='$date' and end_date>='$date' and pid=$pid and ptype=1 and string_bj(weekdays,$onday)=1 order by id desc limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$s_price=$GLOBALS['le']->f('s_price');
			$storage=$GLOBALS['le']->f('storage');
			if (is_numeric($s_price)) {
				if ($get_storage==1) return "$s_price,$storage";
				else return $s_price;
			}
			
			$q_s_price=($ptype==0)?'n_price':'l_price as n_price';
			$str="select $q_s_price,storage from uu_product_price where start_date<='$date' and end_date>='$date' and pid=$pid and ptype=0 and string_bj(weekdays,$onday)=1 order by id desc limit 1";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$n_price=$GLOBALS['le']->f('n_price');
			$storage=$GLOBALS['le']->f('storage');
			if (is_numeric($n_price)) {
				if ($get_storage==1) return "$n_price,$storage";
				else return $n_price;
			}
			
			return -1;
		}elseif($mode==2){
			//单个有效最低价
			$q_s_price=($ptype==0)?'s_price':'l_price';
			$str="select min($q_s_price) as mp from uu_product_price where date_add(end_date,interval 1 day)>now() and pid=$pid and $q_s_price>=0";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$mp=$GLOBALS['le']->f('mp');
			
			$q_s_price=($ptype==0)?'n_price':'l_price';
			$str="select min($q_s_price) as np from uu_product_price where date_add(end_date,interval 1 day)>now() and pid=$pid and $q_s_price>=0";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$np=$GLOBALS['le']->f('np');
			
			$lp=($mp>=$np)?$mp:$np;
			if (is_numeric($lp) && $lp>=0) return $lp;
			else return -1;
		}else{
			$str="select id,pid,ptype,n_price,s_price,l_price,start_date,end_date,memo,weekdays,storage from uu_product_price where pid=$pid";
			if ($sdate!=='' && $edate!=='') $str.=" and greatest(start_date,'$sdate')<=least(end_date,'$edate')";
			$str.=" order by id desc";
			//$a5[]=array("fprice"=>$fprice);
        	$GLOBALS['le']->query($str);
			while ($row=$GLOBALS['le']->fetch_assoc()){
				$a5[]=$row;
			}			
			return toXML($a5);			
		}
	
		//}else{
		//return $this->err1;	
		//}

    }



//景区列表
	public function Get_ScenicSpot_List($ac, $pw, $n=NULL, $m=NULL) {
	
		if ($this->authrize($ac, $pw)){
		$str="select id from uu_distributor where dcode='$ac'";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$fxs_id=$GLOBALS['le']->f('id');
		
		if ($fxs_id){
			$ap=array();
			$str="select tid from uu_priceset where pid='$fxs_id'";	
			$GLOBALS['le']->query($str);
			while ($GLOBALS['le']->fetch_assoc()) {
			$ap[]=$GLOBALS['le']->f('tid');
			}
			
						
			$apstr=implode(',',$ap);
			$aland=array();		
			$str="select landid from uu_jq_ticket where pid in ($apstr) group by landid";	
			$GLOBALS['le']->query($str);
			while ($GLOBALS['le']->fetch_assoc()) {
			$aland[]=$GLOBALS['le']->f('landid');
			}
			
			$alandstr=implode(',',$aland);
			
		}
		
		if (!$alandstr) return $this->err5;
		
		$a1=array();
		$str="select id,title,addtime,imgpath,area from ".$this->t1." where status='1' and id in ($alandstr) order by px desc";
		if ($n) $str.=" limit ".$n;
		if ($m && $n) $str.=",$m";
        $GLOBALS['le']->query($str);
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a1[]=$row;
		}
		//return $str;
		  if (count($a1)<1) {
		     return $this->err5;
		   }else{
			 foreach ($a1 as $key=>$val){  
			  $kword=$val[area];
			  $akword=explode("|",$kword);
			  $apword=array();
			  
			  $str="select area_name from ".$this->t5." where area_id='".$akword[0]."' or area_id='".$akword[1]."' or area_id='".$akword[2]."'";
			  $GLOBALS['le']->query($str);
		      while ($GLOBALS['le']->fetch_assoc()) {
                 $apword[]=$GLOBALS['le']->f('area_name');
		      }
			  //return implode('|',$apword);
			  $a1[$key]['area']=implode('|',$apword);			  
			 }
		     return toXML($a1);
		   }
		
		}else{
		return $this->err1;
		}

    }

//景区详细
	public function Get_ScenicSpot_Info($ac, $pw, $n) {

		if ($this->authrize($ac, $pw)){
		$a2=array();
		$str="select id,title,tel,fax,jtype,opentime,runtime,area,address,jtzn,jdjj,bhjq,yllx,jqts,zbss,zbms,tctj,qt,imgpath from ".$this->t1." where id='$n'";

        $GLOBALS['le']->query($str);
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a2[]=$row;
		}
		//return $str;
		if (count($a2)<1) return $this->err5;
		else return toXML($a2);
		}else{
		return $this->err1;	
		}

    }

//门票查询
	public function Get_Ticket_List($ac, $pw, $n=NULL, $m=NULL) {

		if ($this->authrize($ac, $pw)){
		if (!$n && !$m) return $this->err5;	
		$a3=array();
		$str="select landid,l.title as ltitle,t.id,t.title,tprice,uprice,gprice,delaydays,uniont,pay,notes,wprice,ifs,ddays,pid from ".$this->t2." as t left join ".$this->t1." as l on t.landid=l.id where l.status='1'";
		if ($m) $str.=" and t.id='".(int)$m."'";
		else $str.=" and t.landid='$n'";
		$str.=" order by t.px desc";
        $GLOBALS['le']->query($str);
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a3[]=$row;
		}
		//return $str;
		if (count($a3)<1) return $this->err5;		
		else return toXML($a3);
		}else{
		return $this->err1;	
		}

    }

//特价票信息
	public function Get_Special_Ticket_Info($ac, $pw, $l, $t) {

		if ($this->authrize($ac, $pw)){
		$a4=array();
		$str="select id,lid,tid,ptime,pprice,notes from ".$this->t3." where status='1' and lid='$l' and tid='$t'";
        $GLOBALS['le']->query($str);
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a4[]=$row;
		}
		//return $str;
		if (count($a4)<1) return $this->err5;		
		else return toXML($a4);
		}else{
		return $this->err1;	
		}
    }

/*实时库存查询 商品tid,查询类别（1每日实时库存2实时总库存）,查询时间串逗号分割（例如：2013-09-20,2013-09-21[类别为1时需要]）,总库存启用时间[类别为2时需要], 0返回XML1返回字符串*/
	public function Dynamic_Storage_Check($tid, $dtype, $times=NULL, $nowdate=NULL, $XMLC=0) {
		//if ($ac=$this->authrize()){
		if (!$tid) return $this->err11;

		//每日库存
		if ($dtype==1){
			$ar1=explode(',',$times);
			$axml=array();
			//每日实时库存
			foreach ($ar1 as $k=>$v){
				$str="select sum(tnum) as cc from uu_jq_order where playtime='$v' and tid='$tid' and paystatus<>2 and (status=0 or status=1 or status=6)";
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$cc1=$GLOBALS['le']->f('cc');
				
				$str="select sum(tnum) as cc from uu_ss_order s left join uu_order_fx_details fx on s.ordernum=fx.orderid where s.begintime='$v' and s.tid='$tid' and fx.pay_status<>2 and (s.status=0 or s.status=1 or s.status=6)";
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$cc2=$GLOBALS['le']->f('cc');
				
				$cc=$cc1+$cc2;
				
				$axml[]=$v."|".$cc;
			}
			$xstring=implode(',',$axml);
		}elseif ($dtype==2){
			//$axml=array();
			//实时总库存
			$nowdate=$nowdate?$nowdate:date('Y-m-d');
			$str="select sum(tnum) as cc from uu_jq_order where playtime>='$nowdate' and tid='$tid' and paystatus<>2 and (status=0 or status=1 or status=6)";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$cc1=$GLOBALS['le']->f('cc');
			
			$str="select sum(tnum) as cc from uu_ss_order s left join uu_order_fx_details fx on s.ordernum=fx.orderid where s.begintime>='$nowdate' and s.tid='$tid' and fx.pay_status<>2 and (s.status=0 or s.status=1 or s.status=6)";
			$GLOBALS['le']->query($str);
			$GLOBALS['le']->fetch_assoc();
			$cc2=$GLOBALS['le']->f('cc');
			
			$cc=$cc1+$cc2;
			
			//$axml[]=$cc;
			$xstring=$cc;		
		
		}
		
		
		if ($XMLC==0){
			$a5=array();
			$a5[]=array("Xstr"=>$xstring);
			return toXML($a5);
		}else{
			return $xstring;
		}
		
		
	
		//}else{
		//return $this->err1;	
		//}

    }	
	
	

//订单提交
	public function Ticket_Order_Submit($ac, $pw, $lid, $tid, $rnum, $playtime, $tnum, $tprice, $ordername, $ordertel, $ordertime, $payinfo, $paystatus,$totalmoney, $type=0, $linktStr="", $member="") {
		//	景区(S)、酒店(H)、机票(F)、商品(C)、旅行社(T)

		if ($this->authrize($ac, $pw)){
		//连接判定还无法正确返回PS
		require_once("../conf/pg.le.je");
  		$lee = new pgconn();
		$lee->getConn();
		
		//测试接口过滤
		if ($ac=='cs0001' && $ordername!='测试test') return $this->err3;
		
		$str="select pid,storage,storage_open from ".$this->t2." where id=$tid limit 1";
        $GLOBALS['le']->query($str);
		if (!$GLOBALS['le']->fetch_assoc()) return $this->err5;	
		$pid=$GLOBALS['le']->f('pid');
		$storage=$GLOBALS['le']->f('storage');
		$storage_open=$GLOBALS['le']->f('storage_open');
			
		//商品状态判定
		$str="select id from uu_products where apply_limit=1 and (p_status<>1 or p_status<>6) and id=$pid";
		$GLOBALS['le']->query($str);
		if (!$GLOBALS['le']->fetch_assoc()) return $this->err5;			

		//非先行网结算价格处理
		if ($this->vtype!=1) {
			//结算价动态获取 为了兼容以前的接口门票要先查PID

			//取得先行动态结算价
			$ddprice=$this->get_Dynamic_Price_Merge($pid, $playtime, 1, '', '', 0, 1);			
			if ($ddprice==-1) return $this->err6."1";
			
 			$a_ddprice=explode(',',$ddprice);
			$ddprice=$a_ddprice[0];
			$ddstorage=$a_ddprice[1];
			if ($ddstorage!=-1) {
				//获取实时库存
				$dcstorage=$this->Dynamic_Storage_Check($tid, 1, $playtime, NULL, $XMLC=1);
				$a_dcstorage=explode('|',$dcstorage);
				$d_cstorage=$a_dcstorage[1];
				$d_cstorage=(int)$d_cstorage+$tnum;
				
				if ($d_cstorage>$ddstorage) return $this->err6."2";				
			}
			
			if ($storage!=-1){
				//获取实时总库存
				$t_storage=$this->Dynamic_Storage_Check($tid, 2, NULL, $storage_open, $XMLC=1);	
				$t_storage=(int)$t_storage+$tnum;
				if ($t_storage>$storage) return $this->err6."3";				
			}
			
			
			//取得此分销商的差价
			$str="select id from uu_distributor where dcode='$ac' limit 1";
        	$GLOBALS['le']->query($str);
			if (!$GLOBALS['le']->fetch_assoc()) return $this->err6."4";	
			$d_id=$GLOBALS['le']->f('id');
			
			$str="select dprice from uu_priceset where status=0 and tid='$pid' and pid='$d_id' limit 1";
        	$GLOBALS['le']->query($str);
			if (!$GLOBALS['le']->fetch_assoc()) return $this->err6."5";			
			$dprice=$GLOBALS['le']->f('dprice');
			$tprice=$ddprice+$dprice;
			$tprice=round($tprice/100,2);
		}
		
		if(!preg_match('/^1(2|3|4|5|6|7|8|9)\d{9}$/ ',$ordertel)) return $this->err12;
		//数量必须大于0
		if ($tnum<1) return $this->err5;
		
		//远端订单号不能重复
		$str="select id from ".$this->t4." where status<>'3' and remotenum='$rnum' and fromt='$ac'";
        $GLOBALS['le']->query($str);
		if ($GLOBALS['le']->fetch_assoc()) return $this->err22;	
		if ($type!=0 && $type!=1) return $this->err13;
		
		//这里主要是短信需要
		//if ($lid==-1){
			//$str="select title,salerid,smscontent from ".$this->t21." where pid='$tid'";
        	//$GLOBALS['le']->query($str);
			//if (!$ajq=$GLOBALS['le']->fetch_assoc()) return $this->err6;
			//$atk=array();
		//}else{
			$str="select id,title,delaydays,ddays,getaddr from ".$this->t2." where landid='$lid' and id='$tid'";
        	$GLOBALS['le']->query($str);
			if (!$atk=$GLOBALS['le']->fetch_assoc()) return $this->err6."6";
		
			$str="select id,salerid,title,p_type from ".$this->t1." where id='$lid'";
			$GLOBALS['le']->query($str);
			$ajq=$GLOBALS['le']->fetch_assoc();
			if (!$ajq) return $this->err6."7";
			
			if ($atk['ddays']!=0){
				$nowtime=strtotime(date('Y-m-d'));
				$ddays=$atk['ddays'];
				$excepttime=strtotime("+$ddays days",$nowtime);
				$s_playtime=strtotime($playtime);
				
				if ($s_playtime<$excepttime) return $this->err6."8";
				if ($s_playtime==$excepttime){
					$str="select dhour from uu_land_f where pid='$pid'";
					$GLOBALS['le']->query($str);
					$GLOBALS['le']->fetch_assoc();
					$dhour=$GLOBALS['le']->f('dhour');
					$nowhour=date("H:i:s");
					if ($dhour<$nowhour) return $this->err6."9";
				}
				
				
			}
		//}
		
		
		
		//订单号生成
		$y=date("ym");
		//$str="select max(id) as mid from ".$this->t4." where date_format(addtime,'%y%m')='".$y."'";
		$str="select ordernum from ".$this->t4." where date_format(addtime,'%y%m')='".$y."' order by id desc limit 1";
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$orderid=$GLOBALS['le']->f('ordernum');
		$orderid=substr($orderid,4,10);
		$orderid=hashstr_de($orderid,"120043");
		$orderid++;
		if ($orderid>999999) return $this->err6."10";
		$orderid=(string)$orderid;
		$ordernum=$y.hashstr($orderid,"120043");
		
		if (!$sid=sString($ajq['salerid'])) return $this->err14;
		//类别标尺
		$ch1=600000; //标尺1
		$ch2=800000; //标尺2
		$ch3=1000000; //标尺3
		
		//资源清空机制，用ID来取最新的
		//1~599999是正常的购票
		//600000~799999是集中采购分销
		//800000~999999是联票购买预留

		$str="select certnum as mid from ".$this->t4." where salerid='".$ajq['salerid']."' and status<>4";
		if ($type==1) $str.=" and certnum>=".$ch2;
		elseif ($type==0) $str.=" and certnum<".$ch1;
		$str.=" order by certnum desc limit 1";
		
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$mid=$GLOBALS['le']->f('mid');
		
		//为空机制
		if (!$mid) {
		 if ($type==0) $mid=0;
		 if ($type==1) $mid=$ch2;
		}
		//恒递增
		$mid++;
		
		//清空机制
		if ($mid>=$ch1 && $type==0) {
			$mid=1;
		}elseif ($mid>=$ch3 && $type==1){
		    $mid=$ch2;
		}
		
		//溢出处理
		$str="select id from ".$this->t4." where status<>4 and certnum='$mid' and salerid='".$ajq['salerid']."'";
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$kill_id=$GLOBALS['le']->f('id');
		if ($kill_id){
		$str="update ".$this->t4." set status=4 where id='$kill_id'";
        if (!$GLOBALS['le']->query($str)) return $this->err15;
		}
		
		//联票处理
		if ($type==0) $linktStr="";
		//$lid, $tid, $rnum, $playtime, $tnum, $tprice, $ordername, $ordertel, $ordertime, $payinfo, $totalmoney,		
		//订单处理
		
		$member=$member?$member:$ac;
		$str= "insert ".$this->t4." set ";
		$str.= "member=\"".$member."\",";
		$str.= "ordernum=\"".$ordernum."\",";
		$str.= "certnum=\"".$mid."\",";
		$str.= "remotenum=\"".$rnum."\",";
		$str.= "lid=\"".$lid."\",";	
		$str.= "tid=\"".$tid."\",";
		$str.= "playtime=\"".$playtime."\",";
		$str.= "tnum=\"".$tnum."\",";
		$str.= "tprice=\"".$tprice."\",";
		$str.= "ordername=\"".$ordername."\",";			
		$str.= "ordertel=\"".$ordertel."\",";
		$str.= "fromt=\"".$ac."\",";
		$str.= "ordertime=\"".$ordertime."\",";
		$str.= "payinfo=\"".$payinfo."\",";
		$str.= "paystatus=\"".$paystatus."\",";
		$str.= "totalmoney=\"".$totalmoney."\",";
		$str.= "iflink=\"".$type."\",";
		$str.= "linkstr=\"".$linktStr."\",";
		$str.= "salerid=\"".$ajq['salerid']."\",";
		$str.= "addtime=now()";
        if (!$GLOBALS['le']->query($str)) return $this->err4;
		
		//构造凭证号
		$flag_saler=(int)$sid.substr($ajq['salerid'],1,5);
		$cert=hashstr($mid, $flag_saler);
		
        //订单溢出
		$str="select count(id) as cid from ".$this->t4." where ordernum=\"".$ordernum."\"";
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$ccid=$GLOBALS['le']->f('cid');
		//订单回滚
		if ($ccid>1) {
		$str="delete from ".$this->t4." where ordernum=\"".$ordernum."\" order by id desc limit 1";
        $GLOBALS['le']->query($str);			
		return $this->err5;
		}
		
		//票付通处理
		$pay_mode=$paystatus?$paystatus:0;
		$delayday=(int)$atk['delaydays'];
		$end_ptime0=strtotime($playtime);
		$end_ptime1=strtotime("+".$delayday." day",$end_ptime0);
		$end_ptime=date("Y-m-d",$end_ptime1);

		$pstr="select id from merch_info where compn_id='".$ajq['salerid']."'";
		$lee->pgquery($pstr);
		$lee->pgFetchAssoc();
		$merch_id=$lee->f('id');
		if (!$merch_id) return $this->err18;

		$pstr="select branch_id from term_info where id='".$merch_id."'";
		$lee->pgquery($pstr);
		$lee->pgFetchAssoc();
		$branch_id=$lee->f('branch_id');
		if (!$branch_id) return $this->err18;
		
        $asrc=array();
		$asrc['id']=$ordernum;
		$asrc['src_order']=$branch_id;
		$asrc['src_merch']=$merch_id;
		$asrc['dst_merch']=$ajq['salerid'];
		$asrc['client_name']="'".$ordername."'";
		$asrc['phone_no']="'".$ordertel."'";
		$asrc['order_state']=0;
		$asrc['total_price']=$totalmoney*100;
		$asrc['pay_mode']=$pay_mode;
		$asrc['prepays']=0;
		$asrc['vouch_code']="'".$ajq['salerid'].$cert."'";
		$asrc['order_time']="'".date('Y-m-d')."'";
		if ($delayday==0) $asrc['begin_time']="'".$playtime." 00:00:00'";
		else $asrc['begin_time']="now()";
		$asrc['end_time']="'".$end_ptime." 23:59:59'";
		$asrc['ins_time']="now()";
		$asrc['upd_time']="now()";
		$asrc['tid']=$tid;
		$asrc['tprice']=$tprice*100;
		$asrc['tnum']=$tnum;
		$asrc['pid']=$pid;
		$pstr="insert into order_info (".make_sql($asrc).") values (".make_sql($asrc,2).")";
		$lee->pgquery($pstr);
		
        $as2=array();
		$as2['order_id']=$ordernum;
		$as2['wares_id']=$tid;
		$as2['quantity']=$tnum;
		$as2['wares_state']=0;
		$as2['begin_time']="now()";
		$as2['end_time']="'".$end_ptime." 23:59:59'";
		$as2['accept_time']="now()";
		$as2['verify_time']="now()";
		$as2['ins_time']="now()";
		$as2['upd_time']="now()";
		$as2['tw_price']=$tprice*100;
		$as2['pid']=$pid;

		$pstr="insert into order_wares (".make_sql($as2).") values (".make_sql($as2,2).")";
		$lee->pgquery($pstr); 

		$pstr="select id from wares_info where id='".$tid."'";
		$lee->pgquery($pstr);
		$lee->pgFetchAssoc();
		$pg_tid=$lee->f('id');
        
		$as3=array();
		$as3['id']=$tid;
		$as3['merch_id']=$ajq['salerid'];
		$as3['wares_type']=1;
		$as3['wares_name']="'".$atk['title']."'";
		$as3['wares_desc']="''";
		$as3['wares_unit']="''";
		$as3['wares_price']=$tprice*100;
		$as3['ins_time']="now()";
		$as3['upd_time']="now()";
		$as3['pid']=$pid;
        
		if ($pg_tid) {
		$pstr="update wares_info set ";
		foreach ($as3 as $key=>$val) {
		  if ($key!="id"){
			$pstr.=$key."=".$val;
			if ($key!="pid") $pstr.=",";
		  }
		}
		$pstr.=" where pid='$pid'";
		
		}else{
		$pstr="insert into wares_info (".make_sql($as3).") values (".make_sql($as3,2).")";
		}
		$lee->pgquery($pstr);


		//确认订单状态更新
		$str="select d_status from uu_products where id='".$pid."'";
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$d_status=$GLOBALS['le']->f('d_status');
		$d_stat=(!$d_status)?0:1;
		$str="insert into uu_order_addon set orderid='$ordernum',dstatus=$d_stat";
		if ($ajq['p_type']=='F') $str.=',ifpack=1';
        $GLOBALS['le']->query($str);
		
		//return $str;
		$a5=array();
		$a51=array("ordernum"=>$rnum, "16uorder"=>$ordernum, "lid"=>$lid, "tid"=>$tid, "playtime"=>$playtime, "ordername"=>$ordername, "ordertel"=>$ordertel, "total"=>$totalmoney, "payinfo"=>$payinfo);
		//验证码传递
		$cert_send_array=array('lvmama','sjtrip','ctrip1');
		if (in_array($ac,$cert_send_array)) $a51['certnum']=$cert;
		elseif ($ac=='16ucom') $a51['certnum']=$mid;
		
		$a5[]=$a51;
		if (count($a5)<1) return $this->err5;
		
		$nosms=array('hxship','sjtrip','ctrip1');
        if (!in_array($ac,$nosms)) {
        if ($paystatus==0 || $paystatus==1){
 	    require_once("../tel/tel.php");
		require_once("../tel/order.php");
		
		if ($num>0){
		$str="update ".$this->t4." set remsg=1 where ordernum='$ordernum'";
		$GLOBALS['le']->query($str);
		}else{
		$str="update ".$this->t4." set smserror='$num' where ordernum='$ordernum'";
		$GLOBALS['le']->query($str);
		}		

		}
		}
		
        return toXML($a5);
		}else{
		return $this->err1;	
		}
    }

//门票订单查询
	public function Get_Ticket_Order_List($ac, $pw, $lid=0, $tid=0, $sid="", $stime, $etime, $ordernum="", $memid="") {
		
		if ($this->authrize($ac, $pw)){
		$a1=array();
		if (!$ordernum){
		$str="select member,o.ordernum,certnum,remotenum,o.lid,l.title as ltitle,tid,t.title,playtime,tnum,o.tprice,ordername,ordertel,ordertime,o.status,payinfo,paystatus,totalmoney,iflink,linkstr,o.salerid,t.getaddr,o.paytime,o.ctime,o.dtime from ".$this->t4." as o left join ".$this->t1." as l on o.lid=l.id left join ".$this->t2." as t on o.tid=t.id ";
		
		$str.="where fromt='$ac' ";
		if ($stime) $str.=" and ordertime>='".$stime."'";
		if ($etime) $str.=" and ordertime<='".$etime."'";
		
		if ($lid) $str.=" and o.lid='$lid'";
		if ($tid) $str.=" and o.tid='$tid'";
		
		if ($sid && $ac=="16ucom") $str.=" and o.salerid='$sid'";
		if ($memid) $str.=" and o.member='$memid'";
		$str.=" order by o.ordertime desc";
		
		//return $str;
		try{
		$GLOBALS['le']->query($str);
		}catch (Exception $e){
		return $str;
		}
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a1[]=$row;
		}
		if (count($a1)<1) return $this->err5;
		}else{
		$str="select member,o.ordernum,certnum,remotenum,o.lid,l.title as ltitle,tid,t.title,playtime,tnum,o.tprice,ordername,ordertel,ordertime,o.status,payinfo,paystatus,totalmoney,iflink,linkstr,o.salerid,t.getaddr,o.paytime,o.ctime,o.dtime from ".$this->t4." as o left join ".$this->t1." as l on o.lid=l.id left join ".$this->t2." as t on o.tid=t.id where fromt='$ac' and o.ordernum=\"".htmlspecialchars($ordernum)."\"";			
		$GLOBALS['le']->query($str);
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a1[]=$row;
		}
		if (count($a1)!=1) return $this->err5;
		}
		//return $str;
		return toXML($a1);
		}else{
		return $this->err1;
		}

    }

//取消订单
	public function Cancel_Order($ac, $pw, $ordern) {

		if ($this->authrize($ac, $pw)){
		$a2=array();
		$str="select payinfo, paystatus, tprice, tnum, member, totalmoney, ordernum from ".$this->t4." where (status='0' or status='2') and ordernum='$ordern' and fromt='$ac'";
        $GLOBALS['le']->query($str);
		while ($row=$GLOBALS['le']->fetch_assoc()) {
         $a2[]=$row;
		}
		//return $str;
		if (count($a2)!=1) return $this->err19;
		require_once("../conf/pg.le.je");
  		$lee = new pgconn();
		$lee->getConn();
		
		//判定是否是套票
		$str="select ifpack from uu_order_addon where orderid='$ordern'";
        $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$ifpack=$GLOBALS['le']->f('ifpack');
		$a3=array();
		if ($ifpack==1){
			$str="select orderid from uu_order_addon where ifpack='$ordern'";
			$GLOBALS['le']->query($str);
			while ($GLOBALS['le']->fetch_assoc()) {
				$a3[]=$GLOBALS['le']->f('orderid');
			}
			foreach ($a3 as $k=>$v){
				$pstr="select order_state from order_info where id=".$v;
				$lee->pgquery($pstr);
				$lee->pgFetchAssoc();
				$order_state=$lee->f('order_state');
				if ($order_state==1) return $this->err19;			
			}
		}

        $pstr="select order_state from order_info where id='".$ordern."'";
		$lee->pgquery($pstr);
		$lee->pgFetchAssoc();
		$order_state=$lee->f('order_state');
		if ($order_state!=0 && $order_state!=4) return $this->err19;
		
	    $str="update ".$this->t4." set status='3',ctime=now() where ordernum='$ordern'";
		if (!$GLOBALS['le']->query($str)) $this->err15;
		
		$pstr="update order_info set order_state='2' where id='".$ordern."'";
		$lee->pgquery($pstr);
		
		//先行退款处理(个人和分销商)
		if ($this->vtype==1){
		  $a_member=explode('|',$a2[0]['member']);
		  if ($a_member[0]!='ss' && ($a2[0]['payinfo']==1 || $a2[0]['payinfo']==2) && $a2[0]['paystatus']==1) {
			 //$totalmoney=$a2[0]['tprice']*$a2[0]['tnum'];
			 $totalmoney=$a2[0]['totalmoney'];
	         $str="update uu_member set money=money+$totalmoney where did='".$a_member[0]."'";
			 $GLOBALS['le']->query($str);
			 $dreason="取消订单".$ordern."退款";
			 $totalmoney=$totalmoney*100;
			 
	         $str="select money from uu_member where did='".$a_member[0]."'";
			 $GLOBALS['le']->query($str);
		     if (!$GLOBALS['le']->fetch_assoc()) return $this->err5;
		     $lmoney=$GLOBALS['le']->f('money');
			 $lmoney=$lmoney*100;
			 
			 $str="insert uu_p_money set memid='".$a_member[0]."',money='$totalmoney',lmoney='$lmoney',dreason='$dreason',dtime=now()";
			 $GLOBALS['le']->query($str);
			 
		  }elseif($a_member[0]=='ss' && $a2[0]['payinfo']==1 && $a2[0]['paystatus']==1 && $a_member[1]){
			 $totalmoney=$a2[0]['tprice']*$a2[0]['tnum']*100;
	         $str="update uu_distributor set money=money+$totalmoney where id='".$a_member[1]."'";
			 $GLOBALS['le']->query($str);
			 $dreason="取消订单".$ordern."退款";
			 
	         $str="select money from uu_distributor where id='".$a_ordern[0]['member']."'";
		     $GLOBALS['le']->query($str);
		     $GLOBALS['le']->fetch_assoc();
			 $lmoney=$GLOBALS['le']->f('money');			 
			 
			 $str="insert uu_d_money set distid='".$a_member[1]."',money='$totalmoney',lmoney='$lmoney',dreason='$dreason',dtime=now()";
			 $GLOBALS['le']->query($str);			  
		  }
		}
		//处理套票的取消
		foreach ($a3 as $k=>$v){
			$str="update ".$this->t7." set status='3',ctime=now() where ordernum='$v'";
			if (!$GLOBALS['le']->query($str)) $this->err15;
			
			$pstr="update order_info set order_state='2' where id='".$v."'";
			$lee->pgquery($pstr);		
		}
		
		$a2_2=array(array('ordernum'=>$a2[0]['ordernum']));
		
		return toXML($a2_2);
		}else{
		return $this->err1;	
		}

    }

//短信重发
	public function reSend_MSG($ac, $pw, $ordern) {
		if ($this->authrize($ac, $pw)){
		$str="select lid,tid,remsg,certnum,salerid,ordertel,payinfo,paystatus,tnum,playtime,status from ".$this->t4." where ordernum='$ordern'";
        $GLOBALS['le']->query($str);
		if (!$GLOBALS['le']->fetch_assoc()) return $this->err5;
		$lid=$GLOBALS['le']->f('lid');
		$tid=$GLOBALS['le']->f('tid');
		$remsg=$GLOBALS['le']->f('remsg');
		$certnum=$GLOBALS['le']->f('certnum');
		$salerid=$GLOBALS['le']->f('salerid');
		$ordertel=$GLOBALS['le']->f('ordertel');
		$payinfo=$GLOBALS['le']->f('payinfo');
		$paystatus=$GLOBALS['le']->f('paystatus');
		$tnum=$GLOBALS['le']->f('tnum');
		$playtime=$GLOBALS['le']->f('playtime');
		$status=$GLOBALS['le']->f('status');
		
		if ($remsg>3) return $this->err16;
		if ($status!=0) return $this->err19;
		//构造凭证号
		$cert=hashstr((string)$certnum, (string)$salerid);

        $a5=array();
		
		$str="select id,title,getaddr from ".$this->t2." where landid='$lid' and id='$tid'";
        $GLOBALS['le']->query($str);
		if (!$atk=$GLOBALS['le']->fetch_assoc()) return $this->err6;
	
		$str="select id,salerid,title from ".$this->t1." where id='$lid'";
		$GLOBALS['le']->query($str);
		if (!$ajq=$GLOBALS['le']->fetch_assoc()) return $this->err6;
        
		if ($paystatus==0 || $paystatus==1){
 	    require_once("../tel/tel.php");
		require_once("../tel/order.php");

		if ($num>0) {
		$str="update ".$this->t4." set remsg=remsg+1 where ordernum='$ordern'";
        $GLOBALS['le']->query($str);
		$a5[]=array("16uorder"=>$ordern, "resend"=>($remsg+1));		
		}else{
		$a5[]=array("error"=>telcode($num));
		}
		return toXML($a5);
		}else{
		return $this->err17;
		}

		}else{
		return $this->err1;	
		}

    }

}

?> 